package top.bingo.test;

import java.util.concurrent.locks.ReentrantLock;

/**
 * reentrant lock使用示例
 * @author 25452
 */
public class ReentrantLockDemo implements Runnable {
    public static ReentrantLock lock=new ReentrantLock();
    public static int i=0;
    @Override
    public void run() {
        for (int j = 0; j < 10000; j++) {
            try {
                // 加锁
                lock.lock();
                i++;
            }finally {
                // 释放锁
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLockDemo reentrantLockDemo = new ReentrantLockDemo();
        Thread thread1 = new Thread(reentrantLockDemo);
        Thread thread2 = new Thread(reentrantLockDemo);
        thread1.start();
        thread2.start();
        thread1.join();thread2.join();
        System.out.println(i);
    }
}
